# -*- coding: utf-8 -*-
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from .. import utils, items
import re


class MovieTopSpider(CrawlSpider):
    name = 'movie_top'
    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/top250?start=0&filter=']

    # 翻页规则，
    pageLink = LinkExtractor(allow=(r"start=\d+&filter="))

    # 定义匹配规则，callback不可以使用parse()
    rules = (
        Rule(pageLink, callback='parse_page', follow=True),
    )

    def parse_page(self, response):
        li_list = response.xpath("//*[@id=\"content\"]/div/div[@class=\"article\"]/ol/li")
        item = items.ScrapyDoubanItem()
        for li in li_list:
            item["id"] = re.compile(r"(\d+)").search(utils.is_None(li.xpath("./div/div[@class=\"pic\"]/a/@href"))[0]).group()
            item["ranking"] =  utils.is_None(li.xpath("./div/div[@class=\"pic\"]/em/text()"))[0]
            item["name"] = utils.del_space(utils.del_tag(utils.is_None(li.xpath("div/div[@class=\"info\"]/div[@class=\"hd\"]/a"))[0]))
            item["score"] = utils.is_None(li.xpath("./div/div[@class=\"info\"]/div[@class=\"bd\"]/div/span[@class=\"rating_num\"]/text()"))[0]
            item["num"] = utils.is_None(li.xpath("./div/div[@class=\"info\"]/div[@class=\"bd\"]/div/span[last()]/text()"))[0]
            item["synopsis"] = utils.is_None(li.xpath("./div/div[@class=\"info\"]/div[@class=\"bd\"]/p[@class=\"quote\"]/span/text()"))[0]
            yield item
